<template>
	<label :for="store.userinfo?'none':'login'" class="login" @click="store.userinfo?emit('onClick'):''">
		<slot></slot>
		<button id="login" open-type="getPhoneNumber" @getphonenumber="login" style="display: none;"></button>
	</label>
</template>

<script setup lang="ts">
import { wxlogin } from '../../api';
import { tRequest } from '../../lib/ts/util';
import { useStore } from '../../store';
	
	const store = useStore()
	const emit = defineEmits(['onClick'])
	
	const login = async (e)=>{
		if(e.detail.code){
			await wxlogin({
				code:e.detail.code,
				sf_pid:uni.getStorageSync('sf_pid') || 0,
				xy_pid:uni.getStorageSync('xy_pid') || 0
			})
		  store.getUserinfo().then(res=>{
			  getOpenid()
			  emit('onClick')
		  })
		}
	}
	
	
	const getOpenid = ()=>{
		uni.login({
			success(e) {
				tRequest('get','/api/user/MingetOpenid',{code:e.code})
			}
		})
	}
	
</script>

<style scoped lang="scss">
	.login{
		display: inline-block;
	}

</style>